using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using ApiProject.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

// 网站管理
namespace ApiProject.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class WebsiteManagementController : ControllerBase
    {
        private IRepository<Records> _recordsRespository;
        private IRepository<AboutOur> _aboutOurRespository;

        private IRepository<QrCodes> _qrcodeRespository;

        private readonly IWebHostEnvironment _hostingEnvironment;

        public WebsiteManagementController(IRepository<Records> recordsRespository, IRepository<AboutOur> aboutOurRespository, IWebHostEnvironment hostingEnvironment, IRepository<QrCodes> qrcodeRespository)
        {
            _recordsRespository = recordsRespository;
            _aboutOurRespository = aboutOurRespository;
            _hostingEnvironment = hostingEnvironment;
            _qrcodeRespository = qrcodeRespository;

        }
        //显示网站信息
        //Get /WebsiteManagement/getWebsite/
        [HttpGet, Route("getWebsite")]
        public dynamic Get()
        {
            var websites = _recordsRespository.Table.Where(x => x.IsDelete == false).FirstOrDefault();

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = websites,
                Msg = "获取成功"
            });
        }


        //修改网站信息
        //Post /WebsiteManagement/website/{id}
        [HttpPost, Route("website/{id}")]
        public dynamic Put(int id, NewRecords model)
        {
            var webName = model.WebName.Trim();
            var webAddress = model.WebAddress.Trim();
            var iCP = model.ICP.Trim();
            var policeRecord = model.PoliceRecord.Trim();
            var telephone = model.Telephone.Trim();
            var email = model.Email.Trim();

            if (id == 0)
            {
                if (webName.Length > 0 && webAddress.Length > 0 && iCP.Length > 0 && policeRecord.Length > 0 && telephone.Length > 0 && email.Length > 0)
                {
                    var records = new Records
                    {
                        WebName = webName,
                        WebAddress = webAddress,
                        ICP = iCP,
                        PoliceRecord = policeRecord,
                        Telephone = telephone,
                        Email = email,
                    };
                    _recordsRespository.Insert(records);

                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = records,
                        Msg = "修改网站信息成功"
                    });
                }
                return (new
                {
                    Code = 104,
                    Data = "",
                    Msg = "请确认后重试"
                });
            }
            else
            {
                var o = _recordsRespository.GetById(id);

                if (webName.Length > 0 && webAddress.Length > 0 && iCP.Length > 0 && policeRecord.Length > 0 && telephone.Length > 0 && email.Length > 0)
                {
                    var records = new Records
                    {
                        WebName = webName,
                        WebAddress = webAddress,
                        ICP = iCP,
                        PoliceRecord = policeRecord,
                        Telephone = telephone,
                        Email = email,
                    };
                    _recordsRespository.Insert(records);

                    o.IsDelete = true;
                    o.UpdatedTime = DateTime.Now;
                    _recordsRespository.Update(o);

                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = records,
                        Msg = "修改网站信息成功"
                    });
                }
                return (new
                {
                    Code = 104,
                    Data = "",
                    Msg = "请确认后重试"
                });

            }
        }


        //获得关于我们的数据
        //Get: /WebsiteManagement/aboutour
        [HttpGet, Route("aboutour")]
        public dynamic GetAboutOur()
        {

            var aboutour = _aboutOurRespository.Table.Where(x => x.IsDelete == false).FirstOrDefault();

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = aboutour,
                Msg = "获取成功"
            });
        }



        //修改关于我们的数据
        //Post /WebsiteManagement/aboutour/{id}
        [HttpPost, Route("aboutour/{id}")]
        public dynamic EditAboutOur(int id, NewAboutOur model)
        {

            var aboutour = model.AboutOurContent;

            if (id == 0)
            {
                var newAboutOur = new AboutOur
                {
                    AboutOurContent = aboutour
                };

                _aboutOurRespository.Insert(newAboutOur);

                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = newAboutOur,
                    Msg = "修改网站信息成功"
                });
            }
            else
            {

                var getAboutOur = _aboutOurRespository.GetById(id);

                var newAboutOur = new AboutOur
                {
                    AboutOurContent = aboutour
                };

                _aboutOurRespository.Insert(newAboutOur);

                getAboutOur.IsDelete = true;
                getAboutOur.UpdatedTime = DateTime.Now;

                _aboutOurRespository.Update(getAboutOur);

                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = newAboutOur,
                    Msg = "修改关于我们成功"
                });
            }

        }

        //  添加二维码
        //Post /WebsiteManagement/addcode
        [HttpPost, Route("addcode")]
        public dynamic AddQrCode()
        {

            var files = HttpContext.Request.Form.Files;
            var name = HttpContext.Request.Form["codeName"].ToString();

            var equalsname = _qrcodeRespository.Table.Where(x => x.Qrcode == name).FirstOrDefault();

            if (equalsname == null)
            {
                string webRootPath = _hostingEnvironment.WebRootPath; // ...Api/wwwroot
                string contentRootPath = _hostingEnvironment.ContentRootPath;
                var showfilePath = PicHelper.getFilePath(files, "upload", webRootPath);

                if (showfilePath != "")
                {
                    var qrcode = new QrCodes
                    {
                        Qrcode = name,
                        QrcodePath = showfilePath,
                    };

                    _qrcodeRespository.Insert(qrcode);

                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = qrcode,
                        Msg = "添加图片成功"
                    });
                }
                else
                {
                    return JsonHelper.Serialize(new
                    {
                        Code = 1000,
                        Data = "",
                        Msg = "添加图片失败"
                    });
                }
            }
            else
            {
                if (equalsname.IsDelete == true)
                {
                    string webRootPath = _hostingEnvironment.WebRootPath; // ...Api/wwwroot
                    var showfilePath = PicHelper.getFilePath(files, "upload", webRootPath);

                    if (showfilePath != "")
                    {
                        var qrcode = new QrCodes
                        {
                            Qrcode = name,
                            QrcodePath = showfilePath
                        };

                        _qrcodeRespository.Insert(qrcode);

                        return JsonHelper.Serialize(new
                        {
                            Code = 200,
                            Data = qrcode,
                            Msg = "添加图片成功"
                        });
                    }
                    else
                    {
                        return JsonHelper.Serialize(new
                        {
                            Code = 1000,
                            Data = "",
                            Msg = "添加图片失败"
                        });
                    }
                }
                else
                {
                    return JsonHelper.Serialize(new
                    {
                        Code = 1000,
                        Data = "",
                        Msg = "二维码名称重复，请重新输入"
                    });
                }
            }

        }


        //查找所有的二维码信息
        // Get  /WebsiteManagement/allcode
        [HttpGet, Route("allcode")]
        public dynamic AllCode()
        {

            var qrCode = _qrcodeRespository.Table.Where(x => x.IsDelete == false).ToList();

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = qrCode,
                Msg = "查找成功"
            });
        }


        //是否启用二维码
        //Post /WebsiteManagement/isenablecode/{id}
        [HttpPost, Route("isenablecode/{id}")]
        public dynamic IsEnable(int id, NewQrCode model)
        {
            var qrcode = _qrcodeRespository.GetById(id);

            var num = model.IsEnable;

            if (qrcode != null)
            {
                if (num == false)
                {
                    qrcode.IsEnable = false;
                    qrcode.UpdatedTime = DateTime.Now;
                    _qrcodeRespository.Update(qrcode);
                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = qrcode,
                        Msg = string.Format("已禁用", id)
                    });
                }
                else
                {
                    qrcode.IsEnable = true;
                    qrcode.UpdatedTime = DateTime.Now;
                    _qrcodeRespository.Update(qrcode);
                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = qrcode,
                        Msg = string.Format("已启用", id)
                    });
                }

            }
            else
            {
                return JsonHelper.Serialize(new { Code = 1000, Data = "", Msg = "禁用失败" });
            }
        }


        //修改二维码
        //Put /WebsiteManagement/editcode/${id}
        [HttpPut, Route("editcode/{id}")]
        public dynamic EditQrCode(int id)
        {
            var files = HttpContext.Request.Form.Files;
            var name = HttpContext.Request.Form["codeName"].ToString();

            var newqrCode = _qrcodeRespository.GetById(id);

            string webRootPath = _hostingEnvironment.WebRootPath; // ...Api/wwwroot
            var showfilePath = PicHelper.getFilePath(files, "upload", webRootPath);

            newqrCode.Qrcode = name;
            newqrCode.QrcodePath = showfilePath;
            newqrCode.UpdatedTime = DateTime.Now;

            _qrcodeRespository.Update(newqrCode);

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = newqrCode
            });

        }


        //伪删除二维码
        //Post /WebsiteManagement/deletecode/${id}
        [HttpPost, Route("deletecode/{id}")]
        public dynamic DeleteCode(int id)
        {

            var qrcode = _qrcodeRespository.GetById(id);

            qrcode.IsDelete = true;
            qrcode.UpdatedTime = DateTime.Now;

            _qrcodeRespository.Update(qrcode);

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = qrcode,
                Msg = "删除二维码成功"
            });
        }

    }
}

